const collections = new Set<Error>();
let timeout: any;
const isProd = import.meta.env.PROD;

// 数据上报
export function report() {
  const data = <Error[]>[];
  collections.forEach((error) => {
    data.push(error);
  });
  collections.clear();

  data.forEach((item) => console.error(item));
}

// 错误采集
export function track(error: Error) {
  clearTimeout(timeout);
  collections.add(error);
  if (isProd) {
    timeout = setTimeout(() => report(), 1000);
  } else {
    report();
  }
}

export const monitor = {
  report,
  track,
};
